var name_1 = document.getElementById("name");
var pwd = document.getElementById("pwd");
var code = document.getElementById("code");
var nametip = document.getElementById("nametip");
var nametip1 = document.getElementById("nametip1");
var nametip2 = document.getElementById("nametip2");
var pwdtip = document.getElementById("pwdtip");
var pwdtip1 = document.getElementById("pwdtip1");
var codetip = document.getElementById("codetip");
var reg_name = /^([\u4E00-\uFA29]|[\uE7C7-\uE7F3]|[a-zA-Z0-9]){6,16}$/;
var reg_email = /^[a-zA-Z\d_]+(\.[a-zA-Z\d_]+)*@([\da-zA-Z](-[\da-zA-Z])?)+(\.{1,2}[a-zA-Z]+)+$/;
var reg_pwd = /^[a-zA-Z0-9_]{1}\w{5,17}$/;
var vertificate = [{
	src: "img/log_code_01.jpg",
	code: "dsef"
}, {
	src: "img/log_code_02.jpg",
	code: "dseg"
}, {
	src: "img/log_code_03.jpg",
	code: "dpeg"
}, {
	src: "img/log_code_04.jpg",
	code: "toeu"
}];

name_1.onblur = function(){
	var name_value = name_1.value;
	if (name_value.indexOf(" ") >=0) {
		nametip2.style.display = "block";
		nametip.style.display = "none";
   		name_value = name_value.replace(/\s/g, "");
   		return false;
	}else{
		nametip2.style.display = "none";
	}
	
	if (name_value.trim() == "" || !reg_name.test(name_value) && !reg_email.test(name_value)) {
		nametip.style.display = "block";
		return false;
	}else{
		nametip.style.display = "none";
	}
	
}

pwd.onblur = function(){
 	var pwd_value = pwd.value;
	if (pwd_value.trim() == "" || !reg_pwd.test(pwd_value)) {
		pwdtip1.style.display = "none";
		pwdtip.style.display = "block";
		return false;
	}else{
		pwdtip.style.display = "none";
	}
}

code.onblur = function(){
	var code_value = code.value;
	if (code_value.trim() == "" || code_value.length != 4) {
		codetip.style.display = "block";
		return false;
	} else {
		for (var i = 0; i < vertificate.length; i++) {
			if (vertificate[i].src == vertificate_img.getAttribute("src")) {
				if (code_value.toLowerCase() != vertificate[i].code) {
					codetip.style.display = "block";
					return false;
				}
			}
		}

	}
	codetip.style.display = "none";
}

function subInput() {
	var name_value = name_1.value;
	var pwd_value = pwd.value;
	var code_value = code.value;
	if (name_value.indexOf(" ") >=0) {
		nametip2.style.display = "block";
		nametip.style.display = "none";
   		name = name.replace(/\s/g, "");
   		return false;
	}else{
		nametip2.style.display = "none";
	}

	if (name_value.trim() == "" || !reg_name.test(name_value) && !reg_email.test(name_value)) {
		nametip.style.display = "block";
		return false;
	}else{
		nametip.style.display = "none";
	}
	if (pwd_value.trim() == "" || !reg_pwd.test(pwd_value)) {
		pwdtip.style.display = "block";
		return false;
	}else{
		pwdtip.style.display = "none";
	}

	if (code_value.trim() == "" || code_value.length != 4) {
		codetip.style.display = "block";
		return false;
	} else {
		for (var i = 0; i < vertificate.length; i++) {
			if (vertificate[i].src == vertificate_img.getAttribute("src")) {
				if (code_value.toLowerCase() != vertificate[i].code) {
					codetip.style.display = "block";
					return false;
				}
			}
		}

	}
	codetip.style.display = "none";
	
	var userInfos = getUserLoginInfo("userInfos");
	if (!userInfos) {
		nametip2.style.display = "none";
		nametip.style.display = "none";
		nametip1.style.display = "block";
		return false;
	} else {
		var userData = JSON.parse(userInfos);
		var bool = true;
		for (var i = 0; i < userData.length; i++) {
			if (userData[i].userName == name_1.value || userData[i].email == name_value.toLowerCase()) {
				nametip1.style.display = "none";
				bool = false;
				if (userData[i].password == pwd.value) {
					pwdtip1.style.display = "none";
					window.location.href = "index.html";
					return true;
					break;
				}else{
					pwdtip.style.display = "none";
					pwdtip1.style.display = "block";
				}
			}
		}
		if (bool) {
			nametip1.style.display = "block";
		}
	}
}

function getUserLoginInfo(key) {
	return localStorage.getItem(key);
}

var vertificate_img = document.getElementsByClassName("vertification_code")[0].getElementsByTagName("img")[0];
var change_img = document.getElementsByClassName("change_img")[0].getElementsByTagName("a")[0];

vertificate_img.onclick = function(){
	var randomNum = Math.ceil(Math.random()*4);
	vertificate_img.setAttribute("src","img/log_code_0"+randomNum+".jpg");
}

change_img.onclick = function(){
	var randomNum = Math.ceil(Math.random()*4);
	vertificate_img.setAttribute("src","img/log_code_0"+randomNum+".jpg");
}